//
//  FileExplorer.ViewController.swift
//  SwiftCode
//
//  Created by 于虎拴 on 2023/4/11.
//

import Foundation
import UIKit

extension FileExplorer {
  final class ViewController: UIViewController {
    private let rootPath: String
    private lazy var viewModel = ViewModel()
    
    init() {
      self.rootPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.path ?? ""
      super.init(nibName: nil, bundle: nil)
    }
    
    init(_ rootPath: String) {
      self.rootPath = rootPath
      
      super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
      super.viewDidLoad()
      
      if rootPath.isEmpty { assertionFailure("rootPath must not be empty!") }
      print("rootPath:", rootPath)
      view.backgroundColor = .white
      
      [headerView, tableView].forEach { view.addSubview($0) }
      headerView.snp.makeConstraints { make in
        make.height.equalTo(48)
        make.top.equalTo(view.safeAreaLayoutGuide)
        make.leading.trailing.equalToSuperview()
      }
      tableView.snp.makeConstraints { make in
        make.top.equalTo(headerView.snp.bottom)
        make.leading.trailing.bottom.equalToSuperview()
      }
    }
    
    private lazy var headerView: UIView = {
      let view = UIView()
      [textField, lessThanButton, greaterThanButton].forEach { view.addSubview($0) }
      // right -> left
      greaterThanButton.snp.makeConstraints { make in
        make.size.equalTo(CGSize(width: 34, height: 34))
        make.centerY.equalToSuperview()
        make.trailing.equalToSuperview().offset(-16)
      }
      lessThanButton.snp.makeConstraints { make in
        make.size.equalTo(CGSize(width: 34, height: 34))
        make.centerY.equalToSuperview()
        make.trailing.equalTo(greaterThanButton.snp.leading).offset(-12)
      }
      textField.snp.makeConstraints { make in
        make.centerY.equalToSuperview()
        make.leading.equalTo(16)
        make.trailing.equalTo(lessThanButton.snp.leading).offset(-12)
      }
      return view
    }()
    
    private lazy var textField: UITextField = {
      let textField = UITextField()
      textField.backgroundColor = .gray.withAlphaComponent(0.1)
      textField.textColor = .black
      textField.font = .systemFont(ofSize: 15)
      textField.clearButtonMode = .whileEditing
      textField.borderStyle = .roundedRect
      textField.returnKeyType = .done
      textField.placeholder = "关键字过滤"
      return textField
    }()
    
    private lazy var lessThanButton: UIButton = {
      let button = UIButton()
      button.backgroundColor = .gray
      button.layer.cornerRadius = 4
      button.layer.masksToBounds = true
      button.setTitle("升", for: .normal)
      return button
    }()
    
    private lazy var  greaterThanButton: UIButton = {
      let button = UIButton()
      button.backgroundColor = .gray
      button.layer.cornerRadius = 4
      button.layer.masksToBounds = true
      button.setTitle("降", for: .normal)
      return button
    }()
    
    lazy var tableView: UITableView = {
      let tableView = UITableView(frame: .zero, style: .plain)
      tableView.register(Cell.self, forCellReuseIdentifier: Cell.reusableIdentifier)
      tableView.backgroundColor = .clear
      tableView.tableFooterView = UIView(frame: .zero)
      return tableView
    }()
  }
}
